package ACM.zijie;

/**
 * @Author wwk
 * @DATE 2022/8/28 10:39
 */
import java.util.*;

public class second {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] nums = new int[n+2];  //存值
        int[] foward = new int[n+2];  //前缀和
        int[] last = new int[n+2];  //后缀和
        int[] res = new int[n+2];  //遍历分割点
        for(int i = 1; i <= n; i++){
            nums[i] = scanner.nextInt();
        }
        for(int i = 1; i <= n; i++){
            foward[i]  = Math.max(foward[i-1],0) + nums[i];
            last[i] = Math.max(last[i-1], foward[i]);
        }
        for(int i = n; i >= 1; i--){
            foward[i] = Math.max(foward[i+1],0)+ nums[i];
            res[i] = Math.max(res[i+1], foward[i]);
        }
        int ans = last[n];
        for (int i = 1; i < n; i++) {
            ans = Math.max(ans, last[i]+ res[i+1]);
        }
        System.out.println(ans);
    }
}
